/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.comandos.adrs;

import br.ufc.quixada.adrs.interfaces.Comando;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.model.Prazo;
import br.ufc.quixada.adrs.service.PrazoService;
import br.ufc.quixada.adrs.util.Msg;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Balthazar
 */
public class CmdAlterarConfirmacaoPrazo implements Comando {

    public String executa(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Map<Prazo, Boolean> prazos = (Map<Prazo, Boolean>) session.getAttribute("prazosAdrs");
        String sPrazoId = request.getParameter("prazo_id");
        String confirmacao = request.getParameter("confirmacao");
        Adrs adrs = (Adrs) session.getAttribute("user");
        Long prazoId = Long.parseLong(sPrazoId);
        Boolean confirm = Boolean.parseBoolean(confirmacao);
        Prazo prazo = null;
        for (Prazo pTemp : prazos.keySet()) {
            if (pTemp.getId().compareTo(prazoId) == 0) {
                prazo = pTemp;
                break;
            }
        }

        if (new PrazoService().updateConfirmacao(prazo, adrs, confirm)) {
            session.setAttribute("sucesso", Msg.SUCCESSFULLY_CONFIRMED);
            prazos.remove(prazo);
            prazos.put(prazo, confirm);
        } else {
            session.setAttribute("erro", Msg.OPERATIONAL_ERROR_TOGGLING_CONFIRMACAO);
        }
        return "/adrs/adrs_prazos_agendados.jsp";
    }
}
